<template>
  <header class="header-nav">
    <!-- 左侧 Logo 和标题 -->
    <div class="logo-section">
      <div class="logo">{{ props.logoText || 'Logo' }}</div>
      <h1 class="title">{{ props.title || '系统名称' }}</h1>
    </div>

    <!-- 中间导航链接 -->
    <nav class="nav-links">
      <router-link
        v-for="link in props.navLinks"
        :key="link.path"
        :to="link.path"
        class="nav-link"
        :class="{ active: currentRoute === link.path }"
      >
        {{ link.name }}
      </router-link>
    </nav>

    <!-- 右侧用户菜单 -->
    <div class="user-menu">
      <div class="user-info" @click="toggleUserMenu">
        <div class="user-avatar">{{ props.userInfo?.initial || 'U' }}</div>
        <span class="user-name">{{ props.userInfo?.name || '用户名' }}</span>
        <span class="arrow" :class="{ open: userMenuOpen }">▼</span>
      </div>
      <!-- 下拉菜单 -->
      <div class="dropdown-menu" v-if="userMenuOpen">
        <div
          v-for="item in props.userMenuItems"
          :key="item.key"
          class="dropdown-item"
          @click="handleUserMenuClick(item)"
        >
          {{ item.label }}
        </div>
      </div>
    </div>
  </header>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue';
import { useRoute } from 'vue-router';

// 定义组件属性
const props = defineProps<{
  logoText?: string;
  title?: string;
  navLinks?: Array<{
    name: string;
    path: string;
  }>;
  userInfo?: {
    name: string;
    initial?: string;
  };
  userMenuItems?: Array<{
    key: string;
    label: string;
    action?: () => void;
  }>;
}>();

// 定义组件事件
const emit = defineEmits<{
  (e: 'user-menu-click', item: { key: string; label: string }): void;
}>();

// 获取当前路由
const route = useRoute();
const currentRoute = computed(() => route.path);

// 用户菜单状态
const userMenuOpen = ref(false);

// 切换用户菜单
const toggleUserMenu = () => {
  userMenuOpen.value = !userMenuOpen.value;
};

// 处理用户菜单点击
const handleUserMenuClick = (item: { key: string; label: string; action?: () => void }) => {
  userMenuOpen.value = false;
  if (item.action) {
    item.action();
  }
  emit('user-menu-click', item);
};

// 点击外部关闭菜单
const handleClickOutside = (event: MouseEvent) => {
  const userMenu = document.querySelector('.user-menu');
  if (userMenu && !userMenu.contains(event.target as Node)) {
    userMenuOpen.value = false;
  }
};

// 生命周期钩子
onMounted(() => {
  document.addEventListener('click', handleClickOutside);
});

onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside);
});
</script>

<style scoped>
.header-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
  padding: 0 24px;
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1000;
}

/* Logo 区域 */
.logo-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo {
  width: 32px;
  height: 32px;
  background-color: #1890ff;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  border-radius: 4px;
}

.title {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
  color: #333;
}

/* 导航链接 */
.nav-links {
  display: flex;
  gap: 24px;
}

.nav-link {
  text-decoration: none;
  color: #666;
  font-size: 16px;
  padding: 8px 12px;
  border-radius: 4px;
  transition: all 0.3s;
}

.nav-link:hover {
  color: #1890ff;
  background-color: #e6f7ff;
}

.nav-link.active {
  color: #1890ff;
  font-weight: 500;
  background-color: #e6f7ff;
}

/* 用户菜单 */
.user-menu {
  position: relative;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 6px 12px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f5f5f5;
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background-color: #1890ff;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 500;
}

.user-name {
  font-size: 14px;
  color: #333;
}

.arrow {
  font-size: 12px;
  color: #999;
  transition: transform 0.3s;
}

.arrow.open {
  transform: rotate(180deg);
}

/* 下拉菜单 */
.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 4px;
  background-color: white;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  min-width: 160px;
  z-index: 1001;
}

.dropdown-item {
  padding: 10px 16px;
  font-size: 14px;
  color: #333;
  cursor: pointer;
  transition: background-color 0.3s;
}

.dropdown-item:hover {
  background-color: #f5f5f5;
  color: #1890ff;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-nav {
    padding: 0 16px;
  }

  .title {
    font-size: 16px;
  }

  .nav-links {
    display: none; /* 在移动端可以改为汉堡菜单 */
  }

  .user-name {
    display: none;
  }
}
</style>